package org.example.express.controller;

import jakarta.servlet.http.HttpSession;
import org.example.express.service.impl.ExpressListServiceImpl;
import org.example.model.common.Result;
import org.example.model.domain.express.ExpressList;
import org.example.model.vo.login.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/express")
public class ExpressListController {
    @Autowired
    private ExpressListServiceImpl expressageListServiceImpl;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/loginAPd")
    public Result<ExpressList> loginAPd(@RequestBody LoginVo loginVo, HttpSession session){
        Result<ExpressList> expressListResult = expressageListServiceImpl.loginAPd(loginVo);
        if (expressListResult.getCode() == 200) {
            session.setAttribute("login", expressListResult.getData());
        }
        return expressListResult;
   }
   @PostMapping("/loginPVc")
   public  Result<ExpressList> loginPVc(@RequestBody LoginVo loginVo,HttpSession session){
       Result<ExpressList> expressListResult = expressageListServiceImpl.loginPVc(loginVo);
       if (expressListResult.getCode() == 200) {
           session.setAttribute("login", expressListResult.getData());
       }
       return expressListResult;
    }

    /*发送验证码*/
    @GetMapping("vc/{phone}")
    public Result vc(@PathVariable("phone") String phone) {
        return expressageListServiceImpl.vc(phone);
    }

    /*校验验证码*/
    @GetMapping("vC/{phone}/{code}")
    public Result vc(@PathVariable("phone") String phone, @PathVariable("code") String code) {
        boolean equals = Objects.requireNonNull(redisTemplate.opsForValue().get(phone)).toString().equals(code);
        if (equals) {
            redisTemplate.delete(phone);
            return Result.build(200, "验证码正确");
        }
        return Result.build(500, "验证码错误");
    }

    /*修改密码*/
    @PostMapping("user/userPasswordUpdate")
    public Result userPasswordUpdate(@RequestBody LoginVo loginVo) {
        return expressageListServiceImpl.userPasswordUpdate(loginVo);
    }



    @GetMapping("user/getUserInfoLogin/{registerIp}")
    public Result getUserInfoLogin(@PathVariable("registerIp") String registerIp) {
        return expressageListServiceImpl.getUserInfoLogin(registerIp);
    }





}
